import tkinter as tk
import random


class TimeFrame(tk.Frame):
    def __init__(self, container):
        super().__init__(container)

        font = ("Helvetica", 18)

        self.timeLabel = tk.Label(self, text="00:00:00", font=font)
        self.timeLabel.pack(side=tk.RIGHT, expand=False, ipady=10)

        self.label = tk.Label(self, text="Time:", font=font)
        self.label.pack(side=tk.RIGHT, expand=False, ipady=10)

        # self.update_time()


class OrderFrame(tk.Frame):
    def __init__(self, container):
        super().__init__(container)

        font = ("Helvetica", 18)

        # self.nextButton = tk.Button(self, text="→", font=font)
        # self.button1["command"] = self.order1
        # self.nextButton.pack(side=tk.RIGHT, expand=False, ipady=10)

        self.label = tk.Label(self, text="000", font=font)
        self.label.pack(side=tk.LEFT, expand=False, ipady=10)

        # self.button1 = tk.Button(self, text="<-", font=font)
        # self.button1["command"] = self.order1
        # self.button1.pack(side=tk.RIGHT, expand=False, ipady=10)


class CalcFrame(tk.Frame):
    def __init__(self, container):
        super().__init__(container)

        font = ("Helvetica", 32)

        self.columnconfigure(0, weight=1)
        self.columnconfigure(1, weight=1)
        self.columnconfigure(2, weight=1)
        self.columnconfigure(3, weight=1)
        self.columnconfigure(4, weight=2)
        self.columnconfigure(5, weight=1)
        # self.columnconfigure(6, weight=1)

        self.num1 = tk.Label(self, text="0", font=font)
        self.num1.grid(row=0, column=0)

        self.operator = tk.Label(self, text="✖️", font=font)
        self.operator.grid(row=0, column=1)

        self.num2 = tk.Label(self, text="0", font=font)
        self.num2.grid(row=0, column=2)

        self.equal = tk.Label(self, text="=", font=font)
        self.equal.grid(row=0, column=3)

        self.result = tk.Label(self, text="0", font=font)
        self.result.grid(row=0, column=4)

        self.calcButton = tk.Button(self, text="✓", font=font, fg="red")
        self.calcButton["command"] = self.calculate
        self.calcButton.grid(row=0, column=5)

    def calculate(self):
        x = int(self.num1["text"])
        y = int(self.num2["text"])
        r = int(self.result["text"])
        if r != x * y:
            s = f"{x}✖️{y}={x*y}"
            self.master.solution.config(text=s)

        x, y = self.master.ex.get()
        self.num1.config(text=str(x))
        self.num2.config(text=str(y))
        self.result.config(text="")


class DigitFrame(tk.Frame):
    def __init__(self, container):
        super().__init__(container)

        font = ("Helvetica", 18)

        self.digits = [None] * 10
        for i in range(10):
            self.digits[i] = tk.Button(self, text=str(i), font=font)
            self.digits[i].bind("<Button-1>", inputDigit)
            self.digits[i].grid(row=i // 5, column=i % 5, pady=1)

        self.backButton = tk.Button(self, text="back", font=font)
        self.backButton["command"] = self.back
        self.backButton.grid(row=0, column=6, rowspan=2, sticky=tk.NS, padx=1, pady=1)

    def back(self):
        result = self.master.calcFrame.result
        pre = result["text"]
        if pre == "":
            pre = "0"
        x = int(pre) // 10
        result.config(text=str(x))
        result.update()


def inputDigit(event):
    d = event.widget["text"]
    result = event.widget.master.master.calcFrame.result
    pre = result["text"]
    if pre == "":
        pre = "0"
    x = int(pre) * 10 + int(d)
    result.config(text=str(x))
    result.update()

    # self.master.calcFrame.result.config(text="")
    # self.master.calcFrame.result.update()
    # self.master.calcFrame.result.config(text="")
    # self.master.calcFrame.result.update()


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        # configure the root window
        self.title("My Awesome App")
        self.geometry("400x500")

        # create a container
        self.timeFrame = TimeFrame(self)
        self.timeFrame.pack(side=tk.TOP, expand=False, fill=tk.X)

        self.orderFrame = OrderFrame(self)
        self.orderFrame.pack(side=tk.TOP, expand=False, fill=tk.X)

        self.calcFrame = CalcFrame(self)
        self.calcFrame.pack(side=tk.TOP, expand=True, fill=tk.X)

        self.solution = tk.Label(self, text="", font=("Helvetica", 18))
        self.solution.pack(side=tk.TOP, expand=False, fill=tk.X)

        self.digitFrame = DigitFrame(self)
        self.digitFrame.pack(side=tk.TOP, expand=True)

        self.ex = Exercise()


class Exercise:
    def __init__(self):
        xs = []
        for a in range(1, 10):
            for b in range(1, 10):
                xs.append((a, b))
        random.shuffle(xs)
        self.xs = xs
        self.n = 0

    def get(self):
        r = self.xs[self.n]
        self.n += 1
        if self.n == len(self.xs):
            random.shuffle(self.xs)
            self.n = 0
        return r


if __name__ == "__main__":
    app = App()
    app.mainloop()
